// pages/index/index.js
import QQMapWX from '../../utils/qqmap-wx-jssdk.min.js'
let qqmapsdk;
let app = getApp();

Page({
    data: {
        // 当前所在城市
        city:'...',
        // 城市列表
        cityItem:[],
        item: [
            {
                "text": "所有城市",
                "children": [
                ]
            }
        ],
        // 轮播图地址
        imgUrls: [
            '/images/room1.png',
            '/images/room2.png',
            '/images/room3.png'
        ],
        // 价格分类
        active: 0,
        // 左侧罩着层显示
        show: false
    },
    onLoad: function () {
        // 实例化腾讯地图API核心类
        qqmapsdk = new QQMapWX({
            key: '4LYBZ-E723P-PGJDN-LRTNC-C2MCK-NFBUF'
        });
        this.getAreaList()
        this.selectComponent('#searchRoom').getRoomList()
    },
    onShow: function () {
        this.getDistrict()
    },
    // 左侧遮罩层开启
    addresIsShow: function() {
        this.setData({
            show: true
        });
    },
    // 左侧遮罩层关闭
    onClose: function() {
        this.setData({
            show: false
        });
    },
    // 开通城市选中
    onClickNav({ detail = {} }) {
        this.setData({
            mainActiveIndex: detail.index || 0
        });
    },
    // 选择城市
    onClickItem({ detail = {} }) {
        this.setData({
            activeId: detail.id
        });
        this.setData({
            city: detail.text
        });
    },
    // 页面跳转
    jumpPage: function(e) {
        let url = e.target.dataset.topageurl
        wx.navigateTo({
            url
        })
        return false
    },
    // 获取地理位置
    getDistrict() {
        let that = this;
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy;
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: latitude,
                        longitude: longitude
                    },
                    success: function (res) {
                        let address_component = res.result.address_component
                        that.setData({
                            city: address_component.city
                            // city: address_component.district
                        })
                    }
                })
            }
        })
    },
    // 获取地区列表
    getAreaList(){
        app.get('/getAreaList')
            .then((res) => {
                if (res.status != 200) return
                this.setData({
                    cityItem: res.data
                })
            })
    }
})